package cn.zhentao.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice {
    //设置切入点
    @Pointcut("execution(* cn.zhentao.service.TeacherService.*(..))")
    private void pt(){

    }
    //设置前置通知
    @Before("pt()")
    public void method(JoinPoint jp){
        System.out.println("开始时间："+System.currentTimeMillis());
        //jp:获取切入点方法的实参
        //获取参数数组
        Object[] objs = jp.getArgs();
        System.out.println("前置通知：方法执行之前的参数是：");
        for(Object param:objs){
            System.out.println(param+",");
        }
    }

    //后置通知
    @After("pt()")
    public void doAfter(){
        System.out.println("后置通知，方法执行之后执行");
    }

    //异常通知
    @AfterThrowing(value = "pt()",throwing = "e")
    public void doTrowable(Throwable e){
        System.out.println("异常通知："+e);
    }

    //环绕通知
    @Around("pt()")
    public void doAround(ProceedingJoinPoint pj) throws Throwable {
        Object[] objs = pj.getArgs();
        long start = System.currentTimeMillis();
        System.out.println("环绕通知：方法执行之前"+objs[0]);
        Object result = pj.proceed();//执行切入点方法，获取返回的结果
        long end = System.currentTimeMillis();
        System.out.println("环绕通知：方法执行之后：方法执行时间："+(end-start));
    }
}
